package com.ruoyi.project.app.aspect.handler;

import com.ruoyi.project.app.anno.ValidParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Field;
import java.lang.reflect.Parameter;

public class ReleaseHandler implements Handler {
    @Override
    public boolean handler(AnnotatedElement element, Object obj) {
        Class<?> clazzType = null;
        ValidParam validParam = null;
        if(element instanceof Parameter){
            Parameter  parameter = (Parameter) element;
            clazzType = parameter.getType();
            validParam = parameter.getAnnotation(ValidParam.class);
        }else if(element instanceof Field){
            Field field = (Field) element;
            clazzType = field.getType();
            validParam = field.getAnnotation(ValidParam.class);
        }

        if (clazzType.isAssignableFrom(HttpServletRequest.class) || clazzType.isAssignableFrom(HttpSession.class) ||
                clazzType.isAssignableFrom(HttpServletResponse.class) || validParam == null) {
            //逻辑到这里就是参数已经没必要再检查了，因为满足上述条件之一就没必要在检查参数，应该释放整条链
            return true;
        }
        return false;
    }
}
